package com.ruoyi.mall.service.impl;

import java.util.Collections;
import java.util.List;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.PrimaryKeyIdUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.mall.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.mapper.OrderCartMapper;
import com.ruoyi.mall.domain.OrderCart;
import com.ruoyi.mall.service.IOrderCartService;

/**
 * 购物车Service业务层处理
 * 
 * @author luogoongquan
 * @date 2024-10-12
 */
@Service
public class OrderCartServiceImpl implements IOrderCartService 
{
    @Autowired
    private OrderCartMapper orderCartMapper;

    /**
     * 查询购物车
     * 
     * @param id 购物车主键
     * @return 购物车
     */
    @Override
    public OrderCart selectOrderCartById(Long id)
    {
        return orderCartMapper.selectOrderCartById(id);
    }

    /**
     * 查询购物车列表
     * 
     * @param orderCart 购物车
     * @return 购物车
     */
    @Override
    public List<OrderCart> selectOrderCartList(OrderCart orderCart)
    {
        return orderCartMapper.selectOrderCartList(orderCart);
    }

    /**
     * 新增购物车
     * 
     * @param orderCart 购物车
     * @return 结果
     */
    @Override
    public int insertOrderCart(OrderCart orderCart)
    {
        Long customerId = orderCart.getCustomerId();
        Long productId = orderCart.getProductId();
        OrderCart old = orderCartMapper.selectOrderCartByCustomerIdProductId(customerId, productId);
        if(old!=null){
            old.setNum(old.getNum()+orderCart.getNum());
            old.setUpdateTime(DateUtils.getNowDate());
            old.setUpdateBy(SecurityUtils.getUsername());
            return orderCartMapper.updateOrderCart(old);
        }
        orderCart.setId(PrimaryKeyIdUtils.getSysIdWorker().nextId());
        orderCart.setCreateTime(DateUtils.getNowDate());
        orderCart.setCreateBy(SecurityUtils.getUsername());
        return orderCartMapper.insertOrderCart(orderCart);
    }

    /**
     * 修改购物车
     * 
     * @param orderCart 购物车
     * @return 结果
     */
    @Override
    public int updateOrderCart(OrderCart orderCart)
    {
        orderCart.setUpdateTime(DateUtils.getNowDate());
        return orderCartMapper.updateOrderCart(orderCart);
    }

    /**
     * 批量删除购物车
     * 
     * @param ids 需要删除的购物车主键
     * @return 结果
     */
    @Override
    public int deleteOrderCartByIds(Long[] ids)
    {
        return orderCartMapper.deleteOrderCartByIds(ids);
    }

    /**
     * 删除购物车信息
     * 
     * @param id 购物车主键
     * @return 结果
     */
    @Override
    public int deleteOrderCartById(Long id)
    {
        return orderCartMapper.deleteOrderCartById(id);
    }

    @Override
    public int changeNum(OrderCart orderCart) {
        Integer num = orderCart.getNum();
        if(num<1){
            throw new ServiceException("数量不能小于1");
        }
        orderCart.setUpdateTime(DateUtils.getNowDate());
        orderCart.setUpdateBy(SecurityUtils.getUsername());
        orderCartMapper.updateOrderCart(orderCart);
        return num;
    }

    @Override
    public List<OrderCart> getOrderCartByIds(Long[] ids) {
        return orderCartMapper.selectOrderCartByIds(ids);
    }
}
